<?php
header("Content-type: text/html; charset=utf-8");
set_time_limit(10);
date_default_timezone_set('Asia/Shanghai');


/* 
//处理 pattern 格式问题，才能正常解析json
$apiContent = file_get_contents('F:\360yunpan\www\github.com\openapi-meta\api-metadata\aliyun-api-metadata-alert\2015-08-15\Api\CreateAlert.json');
$order    = array( "\r\n" ,  "\n" ,  "\r", "\t" );
$replace  =  '' ;
$apiContent  =  str_replace ( $order ,  $replace ,  $apiContent );
// echo $apiContent;
// echo '<br/>';

// $apiContent = addslashes($apiContent);
// $apiContent = compress_html($apiContent);
// echo $apiContent;

$arr = json_decode($apiContent, true);
echo '<pre>';
print_r($arr);
echo '<pre>';
 */
 
 
/* 
$logFile = 'stats'.DIRECTORY_SEPARATOR.'wificonnect.' . date ( "Y-m-d-H" ) . '.log';
$fp = @fopen ( $logFile, 'a' );
if (flock ( $fp, LOCK_EX )) { // 进行排它型锁定
	// 格式：时间|**|用户MAC|**|网关ID|**|网关MAC|**|网关IP|**|网关端口|**|用户请求的url
	$data = implode('|**|', $data).PHP_EOL;
	fwrite ( $fp, $data );
	flock ( $fp, LOCK_UN ); // 释放锁定
}
fclose ( $fp ); 
*/
/* 
https://ecs.aliyuncs.com/?Action=StartInstance&
InstanceId=i-instance1&
Format=XML&
Version=2014-05-26&
AccessKeyId=8yO6UJuoh198Aret&
Signature=Pc5WB8gokVn0xfeu%2FZV%2BiNM1dgI%3D&
SignatureMethod=HMAC-SHA1&
SignatureNonce=15215218852396&
SignatureVersion=1.0&
Timestamp=2015-10-28T16:10:00Z
 */

$api = 'http://ecs-cn-hangzhou.aliyuncs.com/?';

function encodeStr($str){
	$val = urlencode($str);
	$val = str_replace('+', '%20', $val);
	$val = str_replace('*', '%2A', $val);
	$val = str_replace('%7E', '~', $val);
	return $val;
}

function encodeParamValue(&$param){
	foreach($param as $key=>$val){
		$param[$key] = encodeStr($val);
	}
}
function makeQueryStr($param){
	$str = '';
	foreach($param as $key=>$val){
		$str.=$key.'='.$val.'&';
	}
	return substr($str, 0,strlen($str)-1);
}

function makeDate(){
	$time=time()-3600*8;
	return date('Y-m-d',$time).'T'.date('H:i:s',$time).'Z';
}

$param = [
	'Action'=>'DescribeRegions',
	'Format'=>'JSON',
	'Version'=>'2014-05-26',
	'AccessKeyId'=>'8yO6UJuoh198Aret',
	'SignatureMethod'=>'HMAC-SHA1',
	'SignatureNonce'=>rand ( 100000, 999999 ).time(),
	'SignatureVersion'=>'1.0',
	'Timestamp'=>makeDate()
];
ksort($param);
encodeParamValue($param);
$StringToSign = 'GET&' .encodeStr('/') . '&' .encodeStr(makeQueryStr($param));
$tmp = hash_hmac ( 'sha1' , $StringToSign , 'YXUljAlWuEhKziw6inG5lMYU3NlNDR&', true);
$Signature = base64_encode( $tmp );
$sign = ['Signature' => $Signature];
encodeParamValue($sign);
$last = makeQueryStr($sign);

$finalUri = $api.makeQueryStr($param).'&'.$last;
echo $finalUri;
echo '<br/>';
echo file_get_contents($finalUri);
die;


//

//echo $StringToSign;
/* echo $api.makeQueryStr($param);



echo '<pre>';
print_r($param);
echo '<pre>'; */


?>


